
--[[
	消息处理

--]]
---------------------------------------------------------
-- class
---------------------------------------------------------
local M = class("Handler")

function M:ctor(name)
    self.users = {}  -- 房间内玩家信息记录
    self.tables = {} --桌子列表
    self.ranks = {} --排行榜

    self.table_cnt = 0 --桌子数
    self.num_player = 0 --玩家数
    self.flow_user_map = {} -- 注意，只一段时间内的房间内变动的玩家（进入房间或者离开房间的玩家）
    self.room_os_type_and_channel = {} --渠道
    self.tb_count_servs = {} --节点对应的桌子数
    self.check_t = 0 --时间限制
    self.room_taxs_today = { pay_taxs = 0, free_taxs = 0} --今日税收
    self.room_taxs_today_flag = false --税收标记
    self.private_table_ids = {} -- 记录私人桌子id，0表示正在创建

    self:register()
end

-------------------------------------------------------------private----------------------------------------------------
function M:register()
    g.register("C2_TS_bet", handler(self, self.onBet)) --下注操作
    g.register("C2_TS_buyinchips", handler(self, self.onBuyChip)) --购买操作
    g.register("jackpot_change", handler(self, self.onJackpotChange)) --jackpot变化了
    --g.register("C2_TS_auto_buy", handler(self, self.onAutoBuyChip)) --自己买入

end

-- 玩家下注
function M:onBet(msg)
    --print("##########################bet########", tostring(msg))
    local uid = msg.mid
    local user = g.user_mgr:getUser(uid)
    if not user then
        return {code = g.error_code.desk_user_err, tid = g.desk_id}
    end
    if user:isRobot() then
        --g.robo_oper:betChip(user)
        return {code = g.error_code.ok, tid = g.desk_id}
    end
    return g.desk:onBet(uid, msg)
end

--买入
function M:onBuyChip(msg)
    local uid = msg.mid
    local chips = msg.chips
    local user = g.user_mgr:getUser(uid)
    if not user then
        g.log:deskErr(uid, "onBuyChip no user", msg)
        return {code = g.error_code.desk_user_err, tid = g.desk_id}
    end
    if not user:isSeating() then
        g.log:deskErr(uid, "onBuyChip no seating", msg)
        return {code = g.error_code.invalid_sid, tid = g.desk_id}
    end
    g.log:deskInfo(uid, "onBuyChip req:", msg)
    local cfg = g.room_cfg
    if chips < cfg.min_buy then
        g.log:deskErr(uid, "onBuyChip chips err chips:%s min_buy:%s max_limit:%s", chips, cfg.min_buy, cfg.max_limit)
        return {code = g.error_code.invalid_room_min_buy, tid = g.desk_id}
    end
    local user_chip = user:getChip()
    if user_chip + chips > 2*cfg.max_buy then
        return {code = g.error_code.invalid_room_max_limit, tid = g.desk_id}
    end
    local err = user:buyChip(chips, "table_buyin")
    if err then return err end

    local state_num = g.desk:getStageNum()
    local arrive = 1
    if not user:isGaming() or state_num > g.desk.state_num[g.desk.s_stage_over] then
        --reset状态时通知所有玩家
        local data = {
            tid = g.desk_id,
            mid = uid,
            seat_chips = user:getChip(),
            seatid = user:getSeatId(),
        }
        g.desk:sendUsers("TS_2C_buyinchipsBc", data)
        arrive = 0
    end
    local res = {code = g.error_code.ok, tid = g.desk_id, arrive = arrive}
    g.log:deskInfo(uid, "onBuyChip res:", res)
    return res
end

--jackpot变化后广播
function M:onJackpotChange(msg)
    if g.desk:getJackpot() ~= msg.result then
        g.desk:setJackpot(msg.result)
        g.desk:sendJackpot()
    end
    return true
end


----买入
--function M:onAutoBuyChip(msg)
--    local uid = msg.mid
--    local chips = msg.chips
--    local state = msg.state
--    local user = g.user_mgr:getUser(uid)
--    if not user then
--        g.log:deskErr(uid, "onBuyChip no user", msg)
--        return {code = g.error_code.desk_user_err, tid = g.desk_id}
--    end
--    user:setAutoBuy(state)
--    user:setAutoBuyChips(chips)
--
--end




return M